﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using Test_Operator_Platform.UI;
using FigKey_TestExec_SL.UserManager;

namespace Test_Operator_Platform
{
    static class Program
    {
        private static ApplicationContext applicationContext;

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (!CheckAppRunState())
                return;
            FrmLogin localLogin = new FrmLogin(false);
            localLogin.TopMost = true;
            localLogin.ShowDialog();
            localLogin.Focus();
            localLogin.Activate();
            if (localLogin.DialogResult == DialogResult.OK)
            {
                MainForm mfrm = new MainForm(FrmLogin.currentUserName, FrmLogin.IsAdmin);
                Application.Run(mfrm);
            }
        }

        private static bool CheckAppRunState()
        {
            System.Threading.Thread.Sleep(100);
            var processName = Process.GetCurrentProcess().ProcessName;
            Process[] processClient = Process.GetProcessesByName(processName);
            if (processClient.Length > 1)
            {
                MessageBox.Show($"已经启动了程序：{processName}.exe!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();
                return false;
            }
            return true;
        }
    }
}
